package com.supreme.aspectserver.service;

import com.supreme.aspectserver.annotation.OrderLockAnno;
import com.supreme.aspectserver.annotation.ParameterAnno;
import com.supreme.aspectserver.annotation.StockLockAnno;
import com.supreme.aspectserver.annotation.TestAnno;
import com.supreme.aspectserver.params.SimpleArg;
import com.supreme.commonboot.exception.BizException;
import com.supreme.commonboot.utils.ReturnEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@TestAnno(value = "test-service")
@Service
public class TestService {

    public String testForSwitch(int type) {
        String result = null;
        switch (type) {
            case 0:
                result = "zero type";
                break;
            case 1:
                result = "one type";
                break;

            case 2:
                result = "two type";
                break;

            case 3:
                result = "three type";
                break;

            default:
                result = "other type";
                break;
        }

        return result;
    }

    public void testAnnoType() {
        System.out.println("testAnnoType");
    }

    @TestAnno(value = "test-anno-method")
    public ReturnEntity<String> testAnnoMethod() {
        System.out.println("testAnnoMethod");
        return new ReturnEntity<String>().setData("testAnnoMethod");
    }

    @TestAnno(value = "test-anno-method-throw")
    public ReturnEntity<String> testAnnoMethodThrow(Integer value) {
        System.out.println("testAnnoMethodThrow");
        if (value < 0) {
            throw new BizException("TestAnnoMethodThrow", -999);
        }
        return new ReturnEntity<String>().setData("testAnnoMethodThrow");
    }

    @Async
    @TestAnno(value = "test-anno-method-async")
    public ReturnEntity<String> testAnnoMethodAsync() {
        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("111 Thread.currentThread().getName() = " + Thread.currentThread().getName());
        return new ReturnEntity<String>().setData("testAnnoMethodAsync");
    }

    public void testAnnoParameter(SimpleArg simpleArg) {
        System.out.println("testAnnoMethod");
    }

    @OrderLockAnno(value = "orderOne")
//    @OrderLockAnno(value = "orderTow")
    @StockLockAnno
    public void testDoubleAnnoMethod() {
        System.out.println("testDoubleAnnoMethod");
    }
}
